package com.hudson.plugin.report.ccheck.datastore;

import com.hudson.plugin.internal.domain.dao.CcheckresultJpaController;
import com.hudson.plugin.internal.domain.entity.Ccheckresult;
import com.hudson.plugin.result.collector.server.datastore.dao.BuildsEntityJpaController;
import com.hudson.plugin.result.collector.server.datastore.dao.HudsonsEntityJpaController;
import com.hudson.plugin.result.collector.server.datastore.dao.ProjectsEntityJpaController;
import com.hudson.plugin.result.collector.server.datastore.dao.ViewsEntityJpaController;
import com.hudson.plugin.result.collector.server.datastore.internal.ReportDBServiceImpl;
import com.hudson.plugin.result.collector.server.datastore.internal.entity.BuildsEntity;
import com.hudson.plugin.result.collector.server.datastore.internal.entity.HudsonsEntity;
import com.hudson.plugin.result.collector.server.datastore.internal.entity.ProjectsEntity;
import com.hudson.plugin.result.collector.server.datastore.internal.entity.ViewsEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Richard
 * Date: 4/26/11
 * Time: 9:22 PM
 * To change this template use File | Settings | File Templates.
 */
public class CCheckDBStore extends ReportDBServiceImpl{
    private final static  Logger logger = LoggerFactory.getLogger(CCheckDBStore.class);


    private HudsonsEntityJpaController hudsonsJpaController;
    private ViewsEntityJpaController viewsEntityJpaController;
    private ProjectsEntityJpaController projectsEntityJpaController;
    private BuildsEntityJpaController buildsEntityJpaController;
    private CcheckresultJpaController ccheckresultJpaController;
    private Map<String,String> ccheckResultJson;

    public Map<String, String> storeToDB(Map<String, Map<String, String>> valuePairsContainer) throws Exception {
       logger.info("!!!!!!!!!!!!!!!!CCheckDBStore!!!!!!!!!!!!!!");
        Map<String,String> values = valuePairsContainer.get("CCheck");
        ccheckResultJson = values;
        super.storeToDB(valuePairsContainer);

        return null;
    }

    @Override
    protected void updatePluginTable(Map<String, String> valuePairs, HudsonsEntity hudsonsEntity, ViewsEntity viewsEntity, ProjectsEntity projectsEntity, BuildsEntity buildsEntity) throws Exception {
        super.updatePluginTable(valuePairs, hudsonsEntity, viewsEntity, projectsEntity, buildsEntity);    //To change body of overridden methods use File | Settings | File Templates.
        Ccheckresult ccheckresult = new Ccheckresult();
        ccheckresult.setBuildid(buildsEntity.getId());
        ccheckresult.setDetails(ccheckResultJson.get("GetDetails"));
        ccheckresult.setDisplayname(ccheckResultJson.get("GetDisplayName"));
        //ccheckresult.setResultreason();
        ccheckresult.setScore(ccheckResultJson.get("GetScore"));
        ccheckresult.setSummary(ccheckResultJson.get("GetSummary"));
        List<Integer> ccheckIds = ccheckresultJpaController.findCCheckResultIdByBuildsId(buildsEntity.getId());
        if(ccheckIds.size()>0){
           ccheckresult.setId(ccheckIds.get(0));
           ccheckresultJpaController.edit(ccheckresult);
        }
        else{
          ccheckresultJpaController.create(ccheckresult);
        }
    }

    public CcheckresultJpaController getCcheckresultJpaController() {
        return ccheckresultJpaController;
    }

    public void setCcheckresultJpaController(CcheckresultJpaController ccheckresultJpaController) {
        this.ccheckresultJpaController = ccheckresultJpaController;
    }
}
